package com.antexpress.controller;

import com.antexpress.common.Response;
import com.antexpress.pojo.MapCity;
import com.antexpress.pojo.MapDistrict;
import com.antexpress.pojo.MapProvince;
import com.antexpress.service.AreaService;
import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/area/")
public class AreaController {
    @Autowired
    private AreaService areaService;
    
    @RequestMapping(value="upload_file.do", method=RequestMethod.POST)
    @ResponseBody
    public Response uploadExcelFile(HttpServletRequest request) throws Exception {
        Response resp = null;
        String realPath = request.getSession().getServletContext().getRealPath("/static/file/");
        File file = new File(realPath);
        if(!file.exists() && !file.isDirectory()) {
            file.mkdir();
        }
        DiskFileItemFactory factory = new DiskFileItemFactory(); 
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(100*1024*1024); //set the maxsize=100MB
        try {
                List<FileItem> items = upload.parseRequest(request);
                Response uploadResponse = areaService.excelUpload(items, realPath);
            } catch (FileUploadException e) {
                e.printStackTrace();
                resp = Response.createByErrorMessage("File upload failed");
            }
        return resp;
    }
    
    @RequestMapping(value="list_all_province.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listAllProvinces(HttpSession session){
        Response resp = null;
        resp = areaService.getAllProvinces();
        return resp;
    }
    
    @RequestMapping(value="list_city_by_provinceID.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listCityByProvinceId(String pid,HttpSession session){
        Response resp = null;
        MapProvince province = (MapProvince) areaService.getProvinceById(pid).getData();
        resp = areaService.getCityByProvinceId(province);
        return resp;
    }

    @RequestMapping(value="list_district_by_cityID.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listDistrictByCityId(String cid,HttpSession session){
        Response resp = null;
        MapCity city = (MapCity) areaService.getCityById(cid).getData();
        resp = areaService.getDistrictByCityId(city);
        return resp;
    }
    
    @RequestMapping(value="list_town_by_districtID.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listTownByDistrictId(String did,HttpSession session){
        Response resp = null;
        MapDistrict district = (MapDistrict) areaService.getDistrictById(did).getData();
        resp = areaService.getTownByDistrictId(district);
        return resp;
    }
    
}
